package iosr11.ui.common;

/*
import iosr11.Service.ServiceLocatorImpl;
import iosr11.Service.StudentService;
import iosr11.data.Student;
import iosr11.data.User;

import java.util.Map; */
import iosr11.Service.ServiceCreator;
import iosr11.Service.ServiceLocatorImpl;
import iosr11.Service.StudentService;
import iosr11.Service.TeacherService;
import iosr11.Service.ProjectGroupService;
import iosr11.Service.UserAuthenticationService;
import iosr11.Service.UserAuthenticationServiceImpl;
import iosr11.data.Student;
import iosr11.data.Teacher;
import iosr11.data.ProjectGroup;

import java.util.logging.Logger;

import com.google.gdata.client.Service;

//import javax.faces.context.FacesContext;

public class CurrentUserBean {
	private Logger logger = Logger.getLogger("iosr.ui.common");
	private StudentService studentService;
	private TeacherService teacherService;
	private ProjectGroupService projectGrpService;
	private Student student;
	private Teacher teacher;
	private boolean isTeacher;
	private String login;
	private String password;
	private boolean isLoggedIn=false;
	
	public CurrentUserBean(){
		try{
			ServiceCreator.createService();
		}catch(Exception e){
			e.printStackTrace();
			logger.severe("Failed to crete services");
			return;
		}
		
		this.studentService = (StudentService)ServiceLocatorImpl.getInstance().getService(Student.class);
		if(this.studentService == null){
			logger.severe("Could not obtain reference to StudentService");
			return;
		}
		this.teacherService = (TeacherService)ServiceLocatorImpl.getInstance().getService(Teacher.class);
		if(this.teacherService == null){
			logger.severe("Could not obtain reference to TeacherService");
			return;
		}
	}
	
	
	public boolean isLoggedIn(){
		return this.isLoggedIn;
	}
	
	public void setLogin(String login){
		this.login=login;
	}

	public String getLogin(){
		return this.login;
	}
	
	public void setPassword(String password){
		this.password=password;
	}
	
	public String getPassword(){
		return "";
	}
	
	public String login(){
		if(studentService==null){
			return "failure";
		}
		
		UserAuthenticationService authService = new UserAuthenticationServiceImpl();
		boolean authenticatedSuccessfully = authService.authenticate(this.login, this.password);
		if(!authenticatedSuccessfully){
			return "login_failure";
		}
		
		this.student = this.studentService.getStudent(login);
		if(this.student==null){
			this.teacher = this.teacherService.getTeacher(login);
			if(this.teacher==null) {
				this.isTeacher = false;
				return "login_failure";
			} else {
				this.isTeacher = true;
				this.isLoggedIn = true;
				return "teacher";
			}
		} else {
			this.isTeacher = false;
			this.isLoggedIn = true;
			return "student";
		}
	}
	
	public String logout() {
		this.isLoggedIn=false;
		this.student=null;
		this.login=null;
		this.password=null;
		return "logout";
	}
		
	public Student getStudent() {
		return this.student;
	}
	
	public Teacher getTeacher() {
		return this.teacher;
	}
	
	public boolean isTeacher() {
		return this.isTeacher;
	}
	
	public boolean isAssignedToProject() {
		if(isTeacher())
			return false;
		else {
			this.projectGrpService = (ProjectGroupService)ServiceLocatorImpl.getInstance().getService(ProjectGroup.class);
			/*if(this.projectGrpService==null){
				logger.severe("Could not obtain reference to ProjectGroupService");
				throw new Exception("Could not obtain reference to ProjectGroupService");
			}*/
			return projectGrpService.hasSubject(this.student.getProjectGrp());
		}
	}
}
